#!/bin/sh

case "$1" in
    help)
        echo simple wrapper over runit for managing xdg autostart shit
        echo
        echo \`autostartctl boot\` to start the thing,
        echo \`autostartctl generate\` to generate services from ~/.config/autostart,
        echo and everything else is directly passed to \`sv\`
        ;;
    boot)
        exec runsvdir ~/.var/autostart
        ;;
    generate)
        files="$(ls "$HOME/.config/autostart")"
        mkdir -p "$HOME/.var/autostart"

        for file in $files; do
            exec="$(grep '^Exec=' < "$HOME/.config/autostart/$file" | sed 's/Exec=//')"
            file_nodesktop="$(echo $file | sed 's/\.desktop$//g')"
            mkdir -p "$HOME/.var/autostart/$file_nodesktop"
            printf "#!/bin/sh\nexec %s" "$exec" > "$HOME/.var/autostart/$file_nodesktop/run"
            chmod +x "$HOME/.var/autostart/$file_nodesktop/run"
        done
        ;;
    *)
        SVDIR="$HOME/.var/autostart" sv $@
        ;;
esac

